package com.example.myday1909d.ui;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toolbar;

import com.example.myday1909d.Base.BaseActivity;
import com.example.myday1909d.Base.Constants;
import com.example.myday1909d.InterestActivity;
import com.example.myday1909d.R;
import com.example.myday1909d.p.ItInfoPresenter;
import com.example.myday1909d.v.ItInfoView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class ItInfoActivity extends BaseActivity <ItInfoPresenter> implements ItInfoView {

    @BindView(R.id.toolBar)
    Toolbar mToolBar;
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R.id.iv)
    ImageView mIv;
    @BindView(R.id.vp)
    ViewPager mVp;
    private ArrayList<DataBean> mTitles;
    private ArrayList<ItInfoItemFragment> mFragments;


    @OnClick(R.id.iv)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv:
                go2InterestSelected();
                break;
        }
    }

    private void go2InterestSelected() {
        if (mTitles != null && mTitles.size()>0){
            Intent intent = new Intent(this, InterestActivity.class);
            intent.putExtra(Constants.DATA,mTitles);
            startActivityForResult(intent,100);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode ==100 && resultCode ==RESULT_OK){
            if (data != null){
                mTitles =  (ArrayList<DataBean>) data.getSerializableExtra(Constants.DATA);
                setVp();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //menu.add()
        getMenuInflater().inflate(R.menu.it_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:

                break;
            case R.id.navi:

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setTab(ChapterTabBean chapterTabBean) {
        //设置tab,创建fragment
        mTitles = chapterTabBean.getData();
        setVp();

    }

    private void setVp() {
        //创建fragmnet
        mFragments.clear();
        for (int i = 0; i < mTitles.size(); i++) {
            DataBean bean = mTitles.get(i);
            if (bean.isSelect()){
                ItInfoItemFragment fragment = ItInfoItemFragment.getInstance(bean.getName(), bean.getId());
                mFragments.add(fragment);
            }
        }
        VpItAdapter adapter = new VpItAdapter(getSupportFragmentManager(), mTitles, mFragments);
        mVp.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mVp);

    }


    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}

